<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <!-- 搜索 -->
        <el-input
          v-model="crud.queryInput.value"
          clearable
          placeholder="输入搜索内容"
          style="width: 200px;"
          class="filter-item"
          @keyup.enter.native="crud.toQuery"
        />
        <el-select
          v-model="crud.queryInput.type"
          clearable
          placeholder="类型"
          class="filter-item"
          style="width: 130px"
        >
          <el-option
            v-for="item in queryTypeOptions"
            :key="item.key"
            :label="item.display_name"
            :value="item.key"
          />
        </el-select>
        <rrOperation :crud="crud" />
      </div>
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission" />
      <!--表单组件-->
      <project-stage-progress-dialog :crud="crud" :is-visible="isVisible" @dialogCancel="dialogCancel" />
      <!--表格渲染-->
      <el-table
        ref="table"
        v-loading="crud.loading"
        :data="crud.data"
        size="small"
        style="width: 100%;"
        :row-class-name="rowClassSequenceNo"
        @selection-change="crud.selectionChangeHandler"
      >
        <el-table-column type="selection" width="55" />
        <el-table-column v-if="columns.visible('sequence')" prop="sequence" label="序号" width="60" />
        <el-table-column v-if="columns.visible('projectBase.projectName')" prop="projectBase.projectName" label="项目名称" />
        <el-table-column v-if="columns.visible('stageName')" prop="stageName" label="项目阶段" />
        <el-table-column v-if="columns.visible('stageDesc')" prop="stageDesc" label="项目阶段描述" width="400" />
        <el-table-column
          v-if="columns.visible('planStartTime')"
          prop="planStartTime"
          label="预期起始时间"
        >
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.planStartTime, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column
          v-if="columns.visible('planFinishTime')"
          prop="planFinishTime"
          label="预期完成时间"
        >
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.planFinishTime, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column v-if="columns.visible('startTime')" prop="startTime" label="实际开始时间">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.startTime, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column v-if="columns.visible('endTime')" prop="endTime" label="实际结束时间">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.endTime, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column v-if="false" prop="ratio" label="阶段在整个项目占比" />
        <el-table-column v-if="columns.visible('workState')" prop="workState" label="工作完成状态" />
        <el-table-column v-if="columns.visible('projectStaff')" fixed="right" prop="projectStaff" label="历史周报" width="80px">
          <template slot-scope="scope">
            <router-link :to="{ path: 'projectstageprogresslist', query: { mode: 'ONE', stageId: scope.row.id, stageName: scope.row.stageName, projectId: scope.row.projectBase.id, projectName: scope.row.projectBase.projectName }}">
              <el-tag>
                查看
              </el-tag>
            </router-link>
          </template>
        </el-table-column>
        <el-table-column
          v-permission="['admin','projectStageProgress:edit']"
          fixed="right"
          label="进度填报"
          width="80px"
          align="center"
        >
          <template slot-scope="scope">
            <el-button v-permission="['admin','projectStageProgress:edit']" :loading="crud.status.cu === 2" size="mini" type="primary" icon="el-icon-edit" @click="openEdit(scope.row)" />
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
import crudProjectStage from '@/api/projectStage'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import pagination from '@crud/Pagination'
import ProjectStageProgressDialog from './projectstageprogressdialog.vue'
// crud交由presenter持有
const defaultCrud = CRUD({ title: '项目阶段', url: 'api/projectStage', sort: 'id,desc', crudMethod: { ...crudProjectStage }, optShow: { add: false, edit: true, del: false, download: true }})
const defaultForm = { id: null, projectId: null, stageName: null, stageDesc: null, planStartTime: null, planFinishTime: null, startTime: null, endTime: null, ratio: null, workState: null }
export default {
  name: 'ProjectStageProgress',
  components: { pagination, crudOperation, rrOperation, ProjectStageProgressDialog },
  mixins: [presenter(defaultCrud), header(), form(defaultForm), crud()],
  data() {
    return {
      isVisible: false,
      currentStageID: 0,
      currentProjectID: 0,
      permission: {
        add: ['admin', 'projectStage:add'],
        edit: ['admin', 'projectStage:edit'],
        del: ['admin', 'projectStage:del']
      },
      rules: {
      },
      queryTypeOptions: [
        { key: 'projectId', display_name: '项目ID' },
        { key: 'stageName', display_name: '项目阶段名称' },
        { key: 'stageDesc', display_name: '项目阶段描述' },
        { key: 'workState', display_name: '阶段工作完成状态' }
      ],
      pageCount: 0
    }
  },
  created() {
    this.crud.query.type = null
    this.crud.queryInput.value = null
    this.crud.query = {}
    // 设置默认搜索条件
    if (this.$route.query.mode === 'ONE') {
      this.crud.queryInput.type = 'projectId'
      this.crud.queryInput.value = this.$route.query.projectId
    }
  },
  methods: {
    // 获取数据前设置好接口地址
    [CRUD.HOOK.beforeRefresh]() {
      if (this.pageCount === 0) {
        if (this.$route.query.mode === 'ONE') {
          this.crud.queryInput.type = 'projectId'
          this.crud.queryInput.value = this.$route.query.projectId
        }
        this.pageCount = this.pageCount + 1
      }
      if (this.crud.queryInput.type && this.crud.queryInput.value) {
        this.crud.query[this.queryInput.type] = this.queryInput.value
      }
      return true
    },
    rowClassSequenceNo({ row, rowIndex }) {
      // 把每一行的索引放进row.id
      row.sequence = rowIndex + 1
    },
    openEdit(data) {
      this.isVisible = true
      this.currentStageID = data.id
      this.currentProjectID = data.projectBase.id
      this.crud.toEdit(data)
    },
    dialogCancel(isVisible) {
      this.isVisible = isVisible
    }
  }
}
</script>

<style scoped>

</style>
